package com.kfm.mybatis.provider;

import com.kfm.mybatis.model.Account;
import org.apache.ibatis.jdbc.AbstractSQL;
import org.apache.ibatis.jdbc.SQL;

public class AccountProvider {

    public static String select(Account account){
        String sql = "select * from account";
        StringBuilder sb = new StringBuilder();
        if (account != null){
            if (account.getId() != null){
                sb.append("and id = " + account.getId());
            }
            if (account.getUid() != null){
                sb.append("and uid = " + account.getUid());
            }
        }

        if (sb.length() > 0){
            sql += " where ";
            String s = sb.toString();
            if (s.startsWith("and")) {
                s = s.replaceFirst("and", "");
            }
            sql += s;
        }

        return sql;
    }

    public static String insert(Account account){
        SQL sql = new SQL();

        if (account.getUid() != null){
//            sql.INTO_COLUMNS("uid");
//            sql.INTO_VALUES(account.getUid().toString());

            sql.VALUES("uid", account.getUid().toString());
        }

        if (account.getMoney() != null){
            sql.INTO_COLUMNS("money");
            sql.INTO_VALUES(account.getMoney().toString());
        }
//        // (uid, money)
//        sql.INTO_COLUMNS("uid", "money");
//        // values(1, 222.22)
//        sql.INTO_VALUES(account.getUid().toString(), account.getMoney().toString());

        // insert into account
        sql.INSERT_INTO("account");

        return sql.toString();
    }
}
